package com.sc.study.exam.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.sc.study.exam.domain.ScQuestion;
import com.sc.study.exam.mapper.ScQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.exam.mapper.ScQuestionOptionMapper;
import com.sc.study.exam.domain.ScQuestionOption;
import com.sc.study.exam.service.IScQuestionOptionService;

/**
 * 题目选项Service业务层处理
 * 
 * @author sc
 * @date 2022-02-27
 */
@Service
public class ScQuestionOptionServiceImpl implements IScQuestionOptionService 
{
    @Autowired
    private ScQuestionOptionMapper scQuestionOptionMapper;

    @Autowired
    private ScQuestionMapper scQuestionMapper;

    /**
     * 批量添加题目选项
     * @param list 题目选项数据集合
     * @return 结果
     */
    @Override
    public int batchInsert(List<ScQuestionOption> list) {
        return scQuestionOptionMapper.batchInsert(list);
    }

    /**
     * 查询题目选项
     * 
     * @param id 题目选项主键
     * @return 题目选项
     */
    @Override
    public ScQuestionOption selectScQuestionOptionById(Long id)
    {
        return scQuestionOptionMapper.selectScQuestionOptionById(id);
    }

    /**
     * 查询题目选项列表
     * 
     * @param scQuestionOption 题目选项
     * @return 题目选项
     */
    @Override
    public List<ScQuestionOption> selectScQuestionOptionList(ScQuestionOption scQuestionOption)
    {
        List<ScQuestionOption> scQuestionOptions = scQuestionOptionMapper.selectScQuestionOptionList(scQuestionOption);

        scQuestionOptions.forEach(item -> {
            item.setScQuestion(scQuestionMapper.selectScQuestionById(item.getQuestionid()));
        });


        return scQuestionOptions;
    }

    /**
     * 新增题目选项
     * 
     * @param scQuestionOption 题目选项
     * @return 结果
     */
    @Override
    public int insertScQuestionOption(ScQuestionOption scQuestionOption)
    {
        return scQuestionOptionMapper.insertScQuestionOption(scQuestionOption);
    }

    /**
     * 修改题目选项
     * 
     * @param scQuestionOption 题目选项
     * @return 结果
     */
    @Override
    public int updateScQuestionOption(ScQuestionOption scQuestionOption) {
        return scQuestionOptionMapper.updateScQuestionOption(scQuestionOption);
    }

    /**
     * 批量删除题目选项
     *
     * @param ids 需要删除的题目选项主键集合
     * @param questionIds ids中的每个 questionId
     * @return 结果
     */
    @Override
    public int deleteScQuestionOptionByIds(Integer[] ids, Integer[] questionIds) {

        //修改掉题目对象中存在 ids 中 的 answer
        for (Integer questionId : questionIds) {
            ScQuestion scQuestion = scQuestionMapper.selectScQuestionById(Long.valueOf(questionId));

            for (Integer id : ids) {
                if(scQuestion.getAnswer().contains(id.toString())){
                    List<String> answerList = new LinkedList<>();
                    for (String s : scQuestion.getAnswer().split(",")) {
                        if(s.trim().equals(id.toString().trim())) continue;

                        answerList.add(s.trim());
                    }

                    scQuestion.setAnswer(answerList.toString().trim().replace('[', ' ').replace(']', ' '));
                    scQuestionMapper.updateScQuestion(scQuestion);
                }
            }
        }

        return scQuestionOptionMapper.deleteScQuestionOptionByIds(ids);
    }

    /**
     * 删除题目选项信息
     * 
     * @param id 题目选项主键
     * @return 结果
     */
    @Override
    public int deleteScQuestionOptionById(Long id)
    {
        return scQuestionOptionMapper.deleteScQuestionOptionById(id);
    }

    /**
     * 使用 in 查询出题目选项信息列表
     * @param ids  题目id值，使用，分割每个id
     * @return
     */
    @Override
    public List<ScQuestionOption> selectScQuestionOptionInId(Integer[] ids) {
        return scQuestionOptionMapper.selectScQuestionOptionInId(ids);
    }

    /**
     * 删除题目选项信息
     *
     * @param ids 题目编号集合
     * @return 结果
     */
    @Override
    public int deleteScQuestionOptionByQuestionIds(Long[] ids) {
        return scQuestionOptionMapper.deleteScQuestionOptionByQuestionIds(ids);
    }

    /**
     * 根据题目id查询出题目选项
     * @param questionId 题目id
     * @return
     */
    @Override
    public List<ScQuestionOption> selectScQuestionOptionByQuestionId(Long questionId) {
        return scQuestionOptionMapper.selectScQuestionOptionByQuestionId(questionId);
    }
}
